var mask = null;

var login_user = null
var stu_id = null
var onechannelid = null;
var src = null;
$(function() { // 初始化内容
	mui.plusReady();
	mui.init();
	tap_init();
	mask = mui.createMask(); //遮罩层

	login_user = JSON.parse(localStorage.getItem('loginUser'));
	datetime_init();
	stu_id = sessionStorage.getItem('stu_id')
	src = sessionStorage.getItem('src')
	if (stu_id != null && stu_id != "" && stu_id != undefined) {
		window.setTimeout(function() {
			loadClient()
			loadService()
			jQuery("#btn_submit_kh").css("display", "none")
		}, 500);
	} else {

		window.setTimeout(function() {
			initClient();
			jQuery("#btn_updata_kh").css("display", "none")
		}, 500);
	}
	if (src == "oldClientList") {
		jQuery("#fp_kh").css("display", "none")


	}
	
	if(src == "resourceList"){
		jQuery("#gj_kh").css("display", "none")
	}

})


function tap_init() {

	jQuery(document).ready(function() {

		jQuery("#onechannelid").change(function() {
			var onechannelid = jQuery("#onechannelid").val()

			gettowChannel(onechannelid, "")
		});

	});


}

function initClient() {
	var data = {};

	console.log("初始划客户上传=====" + JSON.stringify(data));
	jQuery.ajax({
		type: "post",
		headers: {
			'Authorization': login_user.token
		},
		url: serverip + "fxgw/newkh",
		contentType: 'application/json;charset=UTF-8',
		data: JSON.stringify(data),
		dataType: "json",
		timeout: 10000,

		success: function(res) {

			console.log("初始化信息=====" + JSON.stringify(res));
			var onechannelList = res.onechannelList
			jQuery("#id").val(res.stu_List.id)

			jQuery("#onechannelid").find("option").remove();

			var str = "<option value=\"\">请选择</option>";
			for (var j = 0; j < onechannelList.length; j++) {
				str += "<option value=\"" + onechannelList[j].id + "\" >" + onechannelList[j].channelname + "</option>";
			}

			jQuery("#onechannelid").append(str);
		},
		error: function(e) {
			console.log("错误信息====" + JSON.stringify(e));
			mask.close(); //显示遮罩层
			var msg = e.responseJSON.message;
			var code = e.responseJSON.status;
			mui.alert('错误编码' + code + "\n" + "错误信息:" + msg);
			console.log("错误信息====" + JSON.stringify(e));
		}
	});

}


function loadClient() {
	var data = {};
	data['id'] = stu_id
	console.log("获取客户信息=====" + JSON.stringify(data));
	jQuery.ajax({
		type: "post",
		headers: {
			'Authorization': login_user.token
		},
		url: serverip + "kf/loadkh",
		contentType: 'application/json;charset=UTF-8',
		data: JSON.stringify(data),
		dataType: "json",
		timeout: 10000,
		beforeSend: function() {

			mask.show(); //显示遮罩层	
		},
		success: function(res) {
			mask.close(); //显示遮罩层 	  
			console.log("客户信息=====" + JSON.stringify(res));
			var onechannelList = res.onechannelList


			jQuery("#onechannelid").find("option").remove();

			var str = "<option value=\"\">请选择</option>";
			for (var j = 0; j < onechannelList.length; j++) {
				str += "<option value=\"" + onechannelList[j].id + "\" >" + onechannelList[j].channelname + "</option>";
			}

			jQuery("#onechannelid").append(str);
			jQuery("#onechannelid").val(res.khList[0].onechannelid);
			gettowChannel(res.khList[0].onechannelid, res.khList[0].twochannelid)
			loadForm(res.khList[0])

		},
		error: function(e) {
			console.log("错误信息====" + JSON.stringify(e));
			mask.close(); //显示遮罩层
			var msg = e.responseJSON.message;
			var code = e.responseJSON.status;
			mui.alert('错误编码' + code + "\n" + "错误信息:" + msg);
			console.log("错误信息====" + JSON.stringify(e));
		}
	});


}

function saveClient() {

	var data = jQuery("#new_kh_form").serializeObject();
	console.log("新建客户上传信息=====" + JSON.stringify(data));
	jQuery.ajax({
		type: "post",
		headers: {
			'Authorization': login_user.token
		},
		url: serverip + "fxgw/newkh",
		contentType: 'application/json;charset=UTF-8',
		data: JSON.stringify(data),
		dataType: "json",
		timeout: 10000,
		beforeSend: function() {

			mask.show(); //显示遮罩层	
		},
		success: function(res) {


			mask.close(); //显示遮罩层 	  

		},
		error: function(e) {
			console.log("错误信息====" + JSON.stringify(e));
			mask.close(); //显示遮罩层
			var msg = e.responseJSON.message;
			var code = e.responseJSON.status;
			mui.alert('错误编码' + code + "\n" + "错误信息:" + msg);
			console.log("错误信息====" + JSON.stringify(e));
		}
	});
}

function gettowChannel(oneChannelId, towChannelidVal) {
	var data = {};
	data["onechannelid"] = oneChannelId
	console.log("获取二级渠道下拉=====" + JSON.stringify(data));
	jQuery.ajax({
		type: "post",
		headers: {
			'Authorization': login_user.token
		},
		url: serverip + "newxlc_channel/getTwoChannel",
		contentType: 'application/json;charset=UTF-8',
		data: JSON.stringify(data),
		dataType: "json",
		timeout: 10000,
		beforeSend: function() {

			mask.show(); //显示遮罩层	
		},
		success: function(res) {

			mask.close(); //显示遮罩层 	  
			jQuery("#twochannelid").find("option").remove();

			var str = "<option value=\"\">请选择</option>";
			for (var j = 0; j < res.length; j++) {
				str += "<option value=\"" + res[j].id + "\" >" + res[j].channelname + "</option>";
			}

			jQuery("#twochannelid").append(str);
		},
		error: function(e) {
			console.log("错误信息====" + JSON.stringify(e));
			mask.close(); //显示遮罩层
			var msg = e.responseJSON.message;
			var code = e.responseJSON.status;
			mui.alert('错误编码' + code + "\n" + "错误信息:" + msg);
			console.log("错误信息====" + JSON.stringify(e));
		}
	}).done(function() {
		if (towChannelidVal != '') {
			jQuery("#twochannelid").val(towChannelidVal);
		}

	});
}



function satrtFollowList() {

	window.sessionStorage.setItem("stu_id", stu_id);
	window.location.href = "followUp_list.html"
}

function allotSaler() {
	document.querySelector("#modal").classList.add("mui-active");
}


function loadService() {
	var data = {};
	data['page'] = 1;
	data["rows"] = 20;
	data['sort'] = "id"

	console.log("资源列表=====" + JSON.stringify(data));
	jQuery.ajax({
		type: "post",
		headers: {
			'Authorization': login_user.token
		},
		url: serverip + "kfzg/zyxxGrid",
		contentType: 'application/json;charset=UTF-8',
		data: JSON.stringify(data),
		dataType: "json",
		timeout: 10000,
		beforeSend: function() {

			mask.show(); //显示遮罩层	
		},
		success: function(res) {

			mask.close(); //显示遮罩层 	  
			var kf_List = res.kf_List;
			jQuery("#serviceid").find("option").remove();

			var str = "<option value=\"\">请选择</option>";
			for (var j = 0; j < kf_List.length; j++) {
				str += "<option value=\"" + kf_List[j].id + "\" >" + kf_List[j].name + "</option>";
			}

			jQuery("#serviceid").append(str);
		},
		error: function(e) {
			console.log("错误信息====" + JSON.stringify(e));
			mask.close(); //显示遮罩层
			var msg = e.responseJSON.message;
			var code = e.responseJSON.status;
			mui.alert('错误编码' + code + "\n" + "错误信息:" + msg);
			console.log("错误信息====" + JSON.stringify(e));
		}
	});
}

function saveService() {
	var data = {};
	data["ids"] = stu_id;
	data["distype"] = "客服"
	data["serviceid"] = $('#serviceid option:selected').val();
	data["servicename "] = $('#serviceid option:selected').text();
	console.log("分配顾问=====" + JSON.stringify(data));
	jQuery.ajax({
		type: "post",
		headers: {
			'Authorization': login_user.token
		},
		url: serverip + "kfzg/zyfp",
		contentType: 'application/json;charset=UTF-8',
		data: JSON.stringify(data),
		dataType: "json",
		timeout: 10000,
		beforeSend: function() {

			mask.show(); //显示遮罩层	
		},
		success: function(res) {


			mask.close(); //显示遮罩层 	  

			mui.alert("保存成功")
			setTimeout(function() {

				mui.back()
			}, 1000)
		},
		error: function(e) {
			console.log("错误信息====" + JSON.stringify(e));
			mask.close(); //显示遮罩层
			var msg = e.responseJSON.message;
			var code = e.responseJSON.status;
			mui.alert('错误编码' + code + "\n" + "错误信息:" + msg);
			console.log("错误信息====" + JSON.stringify(e));
		}
	});

}

function updateClient() {

	var data = jQuery("#new_kh_form").serializeObject();
	console.log("修改客户上传信息=====" + JSON.stringify(data));
	jQuery.ajax({
		type: "post",
		headers: {
			'Authorization': login_user.token
		},
		url: serverip + "kf/savekh",
		contentType: 'application/json;charset=UTF-8',
		data: JSON.stringify(data),
		dataType: "json",
		timeout: 10000,
		beforeSend: function() {

			mask.show(); //显示遮罩层	
		},
		success: function(res) {

			mask.close(); //显示遮罩层 	  
			mui.alert("修改成功")
			setTimeout(function() {
				mui.back()
			}, 1000)
		},
		error: function(e) {
			console.log("错误信息====" + JSON.stringify(e));
			mask.close(); //显示遮罩层
			var msg = e.responseJSON.message;
			var code = e.responseJSON.status;
			mui.alert('错误编码' + code + "\n" + "错误信息:" + msg);
			console.log("错误信息====" + JSON.stringify(e));
		}
	});
}
